﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReportSystem_XY.Utility
{
  public static  class ToModel
    {
        public static TModel DataRowToModel<TModel>(this DataRow dr)
        {
            Type type = typeof(TModel);
            TModel md = (TModel)Activator.CreateInstance(type);

            foreach(var prop in type.GetProperties())
            {
                prop.SetValue(md, dr[prop.Name]);
            }
            return md;
        }


        public static DataTable ToDataTable(this DataRow[] rows)
        {
            if (rows == null || rows.Length == 0) return null;
            DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构
            foreach (DataRow row in rows)
            {
                tmp.ImportRow(row); // 将DataRow添加到DataTable中
            }
            return tmp;
        }


        public static double GetRandomNumber(double minimum, double maximum, int Len)
        {
            Random random = new Random();
            //注意：不用间隔时间，使用for循环获取，返回的几个随机数会一模一样
            System.Threading.Thread.Sleep(1);
            return Math.Round(random.NextDouble() * (maximum - minimum) + minimum, Len);
        }

    }
}
